#pragma once
#include "Comm.h"
#include "Buffer.h"

#if defined(__GNUC__) && !defined(__CYGWIN__)
#include <bits/local_lim.h>
#endif
#ifndef SEM_VALUE_MAX
#define SEM_VALUE_MAX   (0x7FFFFFFF)
#endif

namespace COMM
{
	class COMM_API ProcessSemaphore
	{
		MYLIB_NONCOPIED_CLASS(ProcessSemaphore);
	public:
		// A named semaphore is identified by a name of the form /somename;
		// normally mounted under "/dev/shm", with names of the form sem.somename.
		// in windows,semName not include "Global\\"
		ProcessSemaphore(const char* semName, unsigned int nInitValue = 0);
		~ProcessSemaphore(void);

		// increase semaphore value.
		// the current semaphore value can not exceed SEM_VALUE_MAX.
		void SetSignal();
		// decrease semaphore value. 
		// block thread while the current semaphore value is zero.
		bool WaitSignal(unsigned int timeoutMs = -1);

	private:
#ifdef COMM_MSVC
		HANDLE m_sem;
#else
		bool Open(unsigned int nInitValue);
		void Close();
		sem_t* m_sem;
		String m_name;
#endif
	};
}


